#!/usr/bin/env python

from __future__ import with_statement

import model
import threading

class Contacts(object):
    '''Contacts class
    
    @author Julot <andy.julot@gmail.com>
    @since Thursday, February 23, 2012. 10:24 AM'''
    
    def __init__(self, view, interactor):
        self.view = view
        self.interactor = interactor
        
        self.new()
    
    def loadModel(self, id):
        return model.Contact.objects.get(id)
    
    def new(self):
        toolBar = self.view.contacts.toolBar
        toolBar.save.enable()
        toolBar.delete.disable()
        form = self.view.contacts.form
        form.name.SetFocus()
        with threading.RLock():
            contact = model.Contact()
            attributes = contact.attributes
            attributes.index = -1
        self.view.contacts.form.model = attributes
    
    def save(self):
        contacts = self.view.contacts
        contacts.toolBar.delete.enable()
        attributes = contacts.form.model
        with threading.RLock():
            if attributes.id:
                contact = self.loadModel(attributes.id) 
            else:
                contact = model.Contact()
            contact.setAttributes(attributes)
            index = attributes.index
            contact.save()
            attributes = contact.attributes
        if index > -1:
            contacts.names.EnsureVisible(index)
            contacts.names.SetStringItem(index, 0, attributes.name)
        else:
            index = contacts.names.InsertStringItem(0, attributes.name)
            contacts.names.SetItemData(index, attributes.id)
            contacts.names.EnsureVisible(index)
        attributes.index = index
        contacts.form.model = attributes
    
    def filter(self, value=None):
        self.view.busyInfo('Loading contacts...')
        with threading.RLock():
            query = model.Contact.objects.query().order_by('name')
            if value:
                # This is using modified autorm with like addition to operator
                # list.
                if value.find('%') < 0:
                    value += '%'
                    q = model.Contact.objects.query().filter(name__like=value)
                    if not q.count(): value = '%' + value
                query.filter(name__like=value)
            self.view.contacts.names.values = query
        self.view.idle()
        
    def show(self, index, contactId):
        toolBar = self.view.contacts.toolBar
        toolBar.save.enable()
        toolBar.delete.enable()
        with threading.RLock():
            contact = self.loadModel(contactId)
            attributes = contact.attributes
            attributes.index = index
            self.view.contacts.form.model = attributes
        
    def delete(self):
        contacts = self.view.contacts
        contacts.toolBar.save.disable()
        contacts.toolBar.delete.disable()
        attributes = contacts.form.model
        contacts.names.EnsureVisible(attributes.index)
        contacts.names.DeleteItem(attributes.index)
        with threading.RLock():
            contact = self.loadModel(attributes.id)
            contact.delete()
        contacts.form.model = model.Contact()